﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

#if NETFX_CORE
using Windows.ApplicationModel.Resources;

#endif

namespace StyleMVVM.Validation.Impl.Fluent.Operators
{
	public class UnaryOperator<T> : BaseOperator<T>, IFluentUnaryOperator<T>
	{
		protected MethodInfo expressionMethod;

#if NETFX_CORE
		protected string InternalGenerateMessage(ResourceLoader fluentResources,
		                                         string resourceName)
		{
			string message = fluentResources.GetString(resourceName);

			if (!string.IsNullOrEmpty(message))
			{
				return string.Format(message, LeftValue.GenerateDisplayString(fluentResources));
			}

			return string.Empty;
		}
#else
		protected string InternalGenerateMessage(string formatMessage)
		{
			if (!string.IsNullOrEmpty(formatMessage))
			{
				return string.Format(formatMessage, LeftValue.GenerateDisplayString(null));
			}

			return string.Empty;
		}
#endif

		public override Expression GenerateExpression(IFluentExpressionProvider propertyExpressionProvider)
		{
			Expression lExpression = LeftValue.GenerateExpression(propertyExpressionProvider);

			if (expressionMethod.IsStatic)
			{
				return Expression.Call(expressionMethod, lExpression);
			}

			throw new Exception("Operation GenerateExpressions must be static");
		}

		public IFluentValueProvider<T> LeftValue { get; set; }
	}
}